<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: fzh<fanzhaohai@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;

class ServiceSeatModel extends BaseModel{

    public $tableName='service_roll_call';

    /**
     * @todo 呼叫页面个人信息 房产信息的修改保存
     * @author fzh<fanzhaohai@hrbjjhh.com>
     * @param $customer_id 用户id
     *        $origin 来源 1=活动推荐 ;2=名单拨打;3=名单拨打中的稍后拨打;4=门票确认提醒;5=活动回访;6=我的老客户筛选中过来的用户
     *        $param array() 通过来源带过来的参数 根据$origin的值传不同的参数
     */
    
    public function callNext($customer_id,$origin,$param=array()){
        if($customer_id==0){
            $this->error='用户id错误';
            return false;
        }
        
        if(!in_array($origin, array(1,2,3,4,5,6))){
            $this->error='来源错误';
            return false;
        }
        
        //构造URL参数
        $data['origin'] = $origin;
        //根据来源获取当前用户的下一个用户的 customer_id
        switch ($origin){
            case 1:   //在活动推荐中的某个任务中获取下一个 customer_id
                $result = $this->getRecommendNext($customer_id,$param);
                //直接返回错误信息
                if(false === $result){
                    return $result;
                }
                //返回数据后，定义重新定义URL地址
                if($result > 0){
                    $url = 'ServiceSeat/myRecommendDial';
                    $data['id'] = $result;
                }else{
                    $url = 'ServiceSeat/myRecommend';
                    $data = $param;
                }
                break;
            case 2: //在新客户名单中获取一个 customer_id
                $result = $this->getRollCallNext($param);
                //直接返回错误信息
                if(false === $result){
                    return $result;
                }
                //返回数据后，定义重新定义URL地址
                $url = 'ServiceSeat/myRollCallDial';
                $data['roll_call_id'] = $result;
                break;
            case 3: //新客户名单中的稍后拨打下一个的客户 customer_id
                $result = $this->getSoonAfterCallNext($customer_id,$param);
                //直接返回错误信息
                if(false === $result){
                    return $result;
                }
                //返回数据后，定义重新定义URL地址
                if($result > 0){
                    $url = 'ServiceSeat/myRollCallDial';
                    $data['id'] = $result;
                }else{
                    $url = 'ServiceSeat/myRollCall';
                    $data = $param;
                }
                break;
            case 4: // 获取门票确认提醒的下一个拨打用户的customer_id
                $result = $this->getTicketConfirmationNext($customer_id,$param);
                //直接返回错误信息
                if(false === $result){
                    return $result;
                }
                //返回数据后，定义重新定义URL地址
                if($result > 0){
                    $url = 'ServiceSeat/ticketConfirmationDial';
                    $data['id'] = $result;
                }else{
                    $url = 'ServiceSeat/ticketConfirmation';
                    $data = $param;
                }
                break;
            case 5: // 获取活动回访的下一个客户的customer_id
                $result = $this->getActivityVisitNext($customer_id,$param);
                
                //直接返回错误信息
                if(false === $result){
                    return $result;
                }
                //返回数据后，定义重新定义URL地址
                if($result > 0){
                    $url = 'ServiceSeat/activityVisitDial';
                    $data['id'] = $result;
                }else{
                    $url = 'ServiceSeat/activityVisit';
                    $data = $param;
                }
                break;
            case 6: //老客户搜索过来的用户
                $result = $this->getQueryCustomerNext($customer_id);
                
                //直接返回错误信息
                if(false === $result){
                    return $result;
                }
                //返回数据后，定义重新定义URL地址
                if($result > 0){
                    $url = 'ServiceSeat/queryCustomerDial';
                    $data['id'] = $result;
                }else{
                    $url = 'ServiceSeat/queryCustomer';
                    $data = $param;
                }
                $data = array(
                    'origin'=>$origin,
                    'id'=>$result
                );
                break;
        }
        
        return U($url,$data);
    }
    
    /**
     * @todo 获取呼叫页面（活动推荐状态下）拨打下一个
     * @author fzh
     * @param int $customer_id 
     *        array $param
     * @return int|false
     */
    public function getRecommendNext($customer_id,$param){
        if(!array_key_exists('recommend_id',$param)){
            $this->error='参数中不存在recommend_id';
            return false;
        }
        
        if(!array_key_exists('is_call',$param)){
            $this->error='参数中不存在is_call';
            return false;
        }
        
        $recommend_id = (int)$param['recommend_id'];
        if($recommend_id<=0){
            $this->error='推荐id错误';
            return false;
        }
        
        $is_call = (int)$param['is_call'];
        if(!in_array($is_call,array(0,1))){
            $this->error='拨打状态错误';
            return false;
        }
        
        if($is_call == 0){
            $field = 'customer_id';
        }else{
            $field = 'later_call_time';
        }
        $order = $field.' asc';
        
        //获取当前用户信息时的条件
        $cur_map['customer_id'] = $customer_id;
        
        $service_recommend_customer = M('service_recommend_customer');
        //查询出当前用户的需要比对的字段
        $cur_customer = $service_recommend_customer->field($field)->where($cur_map)->order($order)->find();
        if(empty($cur_customer)){
            $this->error='当前用户信息错误1';
            return false;
        }
        
        //获取某个任务中的 未拨打 的where条件
        $map['recommend_id'] = $recommend_id;
        $map['is_valid'] = 0;
        $map['is_call'] = $is_call;
        $map[$field] = array('gt',$cur_customer[$field]);
        
        //直接获取下一条数据
        $recommend_customer = $service_recommend_customer->field('customer_id')->where($map)->order($order)->find();
        if(isset($recommend_customer['customer_id'])){
            return $recommend_customer['customer_id'];
        }
        
        return 0;
    }
    
    /**
     * @todo 获取呼叫页面（名单拨打状态下）拨打下一个
     * @author fzh
     * @param array $param
     */
    public function getRollCallNext($param){
        if(!array_key_exists('type',$param)){
            $this->error='参数中不存在type';
            return false;
        }
        
        if(!array_key_exists('roll_call_id',$param)){
            $this->error='参数中不存在roll_call_id';
            return false;
        }
        
        $type = (int)$param['type'];
        if(!in_array($type,array(1,2))){
            $this->error='参数类型错误';
        }
        
        $roll_call_id = (int)$param['roll_call_id'];
        if($roll_call_id<=0){
            $this->error='名单id错误';
        }
        return $roll_call_id;
    }
    
    /**
     * @todo 获取呼叫页面（名单拨打稍后状态下）拨打下一个
     * @author fzh
     * @param int $customer_id
     *        array $param
     */
    public function getSoonAfterCallNext($customer_id,$param){
        if(!array_key_exists('type',$param)){
            $this->error='参数中不存在type';
            return false;
        }
        
        $type = (int)$param['type'];
        if(!in_array($type,array(1,2))){
            $this->error='参数类型错误';
        }
        
        //如果是名单列表中的， 才验证名单id
        if($type==1 && !array_key_exists('roll_call_id',$param)){
            $this->error='参数中不存在roll_call_id';
            return false;
        }
        
        $roll_call_id = (int)$param['roll_call_id'];
        if($type==1 && $roll_call_id<=0){
            $this->error='名单id错误';
        }
        
        $field = 'later_call_time';
        $order = $field.' asc';
         
        //获取当前用户信息时的条件
        $cur_map['customer_id'] = $customer_id;
        $service_roll_call_customer = M('service_roll_call_customer');
        //查询出当前用户的需要比对的字段
        $cur_customer = $service_roll_call_customer->field($field)->where($cur_map)->order($order)->find();
        if(empty($cur_customer)){
            $this->error='当前用户信息错误3';
            return false;
        }
        $map['is_call'] = 1;    //稍后拨打
        $map['is_bind'] = 0;   //未绑定
        $map['hold_member_id'] = UID;  //持有人
        $map['is_valid'] = 0;  //有效
        $map[$field] = array('gt',$cur_customer[$field]);
         
        //直接获取下一条数据
        $roll_call_customer = $service_roll_call_customer->field('customer_id')->where($map)->order($order)->find();
        if(isset($roll_call_customer['customer_id'])){
            return $roll_call_customer['customer_id'];
        }else{
            return 0;
        }
    }
    
    /**
     * @todo 获取呼叫页面（门票确认状态下）拨打下一个
     * @author fzh
     * @param int $customer_id
     *        array $param
     */
    public function getTicketConfirmationNext($customer_id,$param){
        if(!array_key_exists('activity_id',$param)){
            $this->error='参数中不存在activity_id';
            return false;
        }
        
        $activity_id = isset($param['activity_id']) ? (int)$param['activity_id'] : 0;
        $ticket_level = isset($param['ticket_level']) ? (int)$param['ticket_level'] : 0;
        $sign_channel = isset($param['sign_channel']) ? (int)$param['sign_channel'] : 0;
        $is_call = isset($param['is_call']) ? (int)$param['is_call'] : 0;
        $sort = isset($param['sort']) ? (int)$param['sort'] : 0;
        
        //超出给定默认值（下面用到这两个变量，所以没有在规定的值内，必须给定默认值）
        if(!in_array($is_call,array(0,1,2))) $is_call = 0;
        if(!in_array($sort,array(0,1))) $sort = 0;
        
        if($activity_id <= 0){
            $this->error='活动id错误';
            return false;
        }
        
        //获取当前用户信息时的条件
        $cur_map['customer_id'] = $customer_id;
        
        //如果通话记录是稍后拨打或者是已拨打状态时 才有按时间的排序
        switch ($is_call){
            case 0:
                $field = 'activity_join_id';
                break;
            case 1:
                $field = 'later_call_time';
                break;
            case 2:
                $field = 'last_call_time';
                break;
            default:
                $field = 'activity_join_id';
        }
        if($sort == 0){
            $order = $field.' asc';
        }
        
        if($sort == 1){
            $order = $field.' desc';
        }
        
        //查询出当前用户的需要比对的字段
        $cur_customer = D('ServiceActivityJoinView')->field($field)->where($cur_map)->order($order)->find();
        if(empty($cur_customer)){
            $this->error='当前用户信息错误4';
            return false;
        }
        if($sort == 0){
            $map[$field] = array('gt',$cur_customer[$field]);
        }
        
        if($sort == 1){
            $map[$field] = array('lt',$cur_customer[$field]);
        }
        
        //查询条件
        $map['member_id'] = UID;
        $map['activity_id'] = $activity_id;
        if(in_array($sign_channel, array(0,1,2,3,4))) $map['sign_channel'] = $sign_channel;
        if(in_array($ticket_level, array(0,1))) $map['ticket_level'] = $ticket_level;
        $map['is_valid'] = 0;
        if(in_array($is_call, array(0,1,2))) $map['is_call'] = $is_call;
        
        //直接获取下一条数据
        $service_activity_join=D('ServiceActivityJoinView')->field('customer_id')->where($map)->order($order)->find();
        if(isset($service_activity_join['customer_id'])){
            return $service_activity_join['customer_id'];
        }else{
            return 0;
        }
    }
    
    /**
     * @todo 获取呼叫页面（活动回访状态下）拨打下一个
     * @author fzh
     * @param int $customer_id
     *        array $param
     */
    public function getActivityVisitNext($customer_id,$param){
        if(!array_key_exists('activity_id',$param)){
            $this->error='参数中不存在activity_id';
            return false;
        }
        
        $activity_id = isset($param['activity_id']) ? (int)$param['activity_id'] : 0;
        $ticket_level = isset($param['ticket_level']) ? (int)$param['ticket_level'] : 0;
        $sign_channel = isset($param['sign_channel']) ? (int)$param['sign_channel'] : 0;
        $is_call = isset($param['is_call']) ? (int)$param['is_call'] : 0;
        $sort = isset($param['sort']) ? (int)$param['sort'] : 0;
        $is_signin = isset($param['is_signin']) ? (int)$param['is_signin'] : 0;
        $is_order = isset($param['is_order']) ? (int)$param['is_order'] : 0;
        
        //超出给定默认值（下面用到这两个变量，所以没有在规定的值内，必须给定默认值）
        if(!in_array($is_call,array(0,1,2))) $is_call = 0;
        if(!in_array($sort,array(0,1))) $sort = 0;
        
        if($activity_id <= 0){
            $this->error='活动id错误';
            return false;
        }
        
        //获取当前用户信息时的条件
        $cur_map['customer_id'] = $customer_id;
        
        //如果通话记录是稍后拨打或者是已拨打状态时 才有按时间的排序
        switch ($is_call){
            case 0:
                $field = 'activity_join_id';
                break;
            case 1:
                $field = 'later_call_time';
                break;
            case 2:
                $field = 'last_call_time';
                break;
            default:
                $field = 'activity_join_id';
        }
        if($sort==0){
            $order = $field.' asc';
        }
        
        if($sort==1){
            $order = $field.' desc';
        }
        
        $ServiceActivityJoin = D('ServiceActivityJoinView');
        //查询出当前用户的需要比对的字段
        $cur_customer = $ServiceActivityJoin->field($field)->where($cur_map)->order($order)->find();
        
        if(empty($cur_customer)){
            $this->error='当前用户信息错误5';
            return false;
        }
        if($sort==0){
            $map[$field] = array('gt',$cur_customer[$field]);
        }
        
        if($sort==1){
            $map[$field] = array('lt',$cur_customer[$field]);
        }
        
        $map['member_id'] = UID;
        $map['activity_id'] = $activity_id;
        if(in_array($sign_channel, array(0,1,2,3,4))) $map['sign_channel'] = $sign_channel;
        if(in_array($ticket_level, array(0,1))) $map['ticket_level'] = $ticket_level;
        $map['is_valid'] = 0;
        if(in_array($is_call, array(0,1,2))) $map['is_call'] = $is_call;
        if(in_array($is_signin, array(0,1))) $map['is_signin'] = $is_signin;
        if(in_array($is_order, array(0,1))) $map['is_order'] = $is_order;
        
        //直接获取下一条数据
        $service_activity_join=$ServiceActivityJoin->field('customer_id')->where($map)->order($order)->find();
        if(isset($service_activity_join['customer_id'])){
            return $service_activity_join['customer_id'];
        }else{
            return 0;
        }
    }
    
    /**
     * @todo 获取呼叫页面（我的老客户状态下）拨打下一个
     * @author fzh
     * @param int $customer_id
     */
    public function getQueryCustomerNext($customer_id){
        if($customer_id <= 0){
            $this->error='用户id错误';
            return false;
        }
        $order = 'call_time asc,create_time desc,customer_id desc';
        $map['member_id'] = UID;
        $map['customer_id'] = array('lt',$customer_id);
        
        $ServiceMyCustomer = M('service_my_customer');
        // 获取分配时间
        $time = $ServiceMyCustomer->where(array('customer_id' => $customer_id, 'member_id' => UID))->getField('create_time');
        $map['create_time'] = (int)$time;
        // 上次拨打时间记录
        $ServiceMyCustomer->where(array('customer_id' => $customer_id, 'member_id' => UID))->setField('call_time', NOW_TIME);
        // 下一个老客户数据调取
        $customer = $ServiceMyCustomer->field('customer_id')->where($map)->order($order)->find();
        if(isset($customer['customer_id'])){
            return $customer['customer_id'];
        }else{
            return 0;
        }
    }
    
    /**
     * @todo 呼叫日志
     * @author fzh
     * @since 2015-11-4
     * @param $table  string 操作的表 array('service_recommend_customer','service_roll_call_customer','service_activity_join')
     *        $customer_id  int 用户id
     *        $recommend_id int 活动推荐id
     *        $later_call_time stirng 稍后拨打时间
     */
    public function dialLog($table,$submit_type,$customer_id,$recommend_id,$later_call_time=''){
        $submit_type = (int)$submit_type;
        $customer_id = (int)$customer_id;
        $recommend_id = (int)$recommend_id;
    
        if(!in_array($table,array('service_recommend_customer','service_roll_call_customer','service_activity_join'))){
            $this->error = '不允许操作的数据表';
            return false;
        }
    
        if(!in_array($submit_type,array(1,2,3,4))){
            $this->error = '保存类型错误';
            return false;
        }
    
        if($customer_id == 0){
            $this->error='用户id错误';
            return false;
        }
        
        if($table == 'service_recommend_customer' && $recommend_id == 0){
            $this->error = '推荐列表id错误';
            return false;
        }
        
        if($submit_type == 1){
            $later_call_time = strtotime($later_call_time);
            if(!$later_call_time){
                $this->error = '请输入稍后拨打时间';
                return false;
            }
            
            if($later_call_time < time()){
                $this->error = '回访时间不能小于当前时间';
                return false;
            }
        }
        
        //where条件
        $map['customer_id'] = $customer_id;
        if($table == 'service_recommend_customer'){
            $map['recommend_id'] = $recommend_id;
        }
    
        //data[] 需要保存的数据
        switch ($submit_type){
            case 1: //稍后拨打，拨打下一个
                $data['is_call'] = 1;
                $data['later_call_time'] = $later_call_time;
                $call_state = 1; //呼叫记录的状态
                break;
            case 2: //无效，拨打下一个
                $data['is_call'] = 2;
                $data['is_valid'] = 1;
                $call_state = 2; //呼叫记录的状态
                break;
            case 3: //跳过，拨打下一个(暂时不用)
                break;
            case 4: //保存，拨打下一个
                $data['is_call'] = 2;
                $call_state = 0; //呼叫记录的状态
                break;
        }
        if($table == 'service_activity_join'){
            $data['last_call_time'] = NOW_TIME;
        }
        
        $obj = M($table)->where($map)->save($data);
        if($obj === false){
            $this->error='保存失败，请重试';
            return false;
        }else{
            //无效时 先把客服系统用户详细信息 中 置为无效。
            if($submit_type == 2){
                M('service_customer_detail')->where(array('customer_id'=>$customer_id))->save(array('is_valid'=>1));
            }
            //保存呼叫记录  call_state 0-正常拨打;1-稍后拨打;2-无效
            D('ServiceApi')->callLog($customer_id,0,$call_state);
            S('service_customer_detail',null);
            return true;
        }
    
    }

    
    
    /**
     * @todo 保存用户班车需求表
     * @author fzh<fanzhaohai@hrbjjhh.com>
     * @since 2015-9-7
     * @param $customer_id 用户id
     *        $activity_id 活动id
     *        $member_id 员工id
     *        $bus_point_id 班车id
     *        array $departure_time_id 发车时间
     *        array $people_num 乘车人数
     *
     */
    public function saveActivityJoinBusDemand($customer_id,$activity_id,$member_id,$bus_point_id=0,$departure_time_id=array(),$people_num=array()){
        
        if($customer_id==0){
            $this->error='用户id错误';
            return false;
        }

        if($activity_id==0){
            $this->error='活动id错误';
            return false;
        }

        if($member_id==0){
            $this->error='员工id错误';
            return false;
        }

        if($bus_point_id==0){
            $this->error='没有选择班车点';
            return false;
        }
        
        $data=array();
        if(is_array($departure_time_id)){
            foreach ($departure_time_id as $val){
                if((int)$val==0){
                    $this->error='班车时间id非法';
                    return false;
                }
                if(!isset($people_num[$val]) || (int)$people_num[$val]==0 || (int)$people_num[$val]>=100){
                    $this->error='乘车人数非法';
                    return false;
                }else{
                    $people_num_cur=$people_num[$val];
                }
    
                $data[]=array(
                    'activity_id'=>$activity_id,
                    'member_id'=>$member_id,
                    'customer_id'=>$customer_id,
                    'bus_point_id'=>$bus_point_id,
                    'departure_time_id'=>$val,
                    'people_num'=>$people_num_cur
                );
            }
        }
        
        if(empty($data)){
            $this->error='班车数据为空';
            return false;
        }else{
            //先删除装修需求
            M('service_activity_join_bus_demand')->where(array('customer_id'=>$customer_id,'activity_id'=>$activity_id))->delete();
            if(false === M('service_activity_join_bus_demand')->addAll($data)){
                $this->error='班车信息添加出错';
                return false;
            }else{
                D('ServiceApi')->operationLog($customer_id,7,'班车需求修改['.getActivityName($activity_id).']');
                return true;
            }
        }
    }

    /**
     * @todo 通过新客户名单id获取 一个 customer_id
     * @author fzh<fanzhaohai@hrbjjhh.com>
     * @since 2015-9-7
     */
    public function getRollCallCustomerId($roll_call_id){

        if((int)$roll_call_id == 0){
            $this->error='名单id错误';
            return false;
        }

        $where=array(
            'is_call'=>0,
            'is_bind'=>0,
            'is_valid'=>0,
            'roll_call_id'=>$roll_call_id,
            'hold_member_id'=>array('in',array(0,UID)), // 避免坐席刷新呼叫页面时，再次取出一个新客，所以要查询出 持有的并没有通话记录的客户
            'bind_member_id'=>0
        );
        $roll_call_customer=M('service_roll_call_customer');
        $customer=$roll_call_customer->field('customer_id,hold_member_id')->where($where)->find();

        if(!empty($customer)){
            $customer_id=$customer['customer_id'];
            //新客户名单抢占，抢占成功  转为持有
            if($customer['hold_member_id']==0){
                M('service_roll_call_customer')->where(array('roll_call_id'=>$roll_call_id,'customer_id'=>$customer_id))->save(array('hold_member_id'=>UID));
            }
            return $customer_id;
        }else{
            $this->error='名单中已经没有资源了';
            return false;
        }
    }


    /**
     * @todo 获取已经报过名的
     * @author fzh<fanzhaohai@hrbjjhh.com>
     * @since 2015-9-8
     */
    public function historyActivity($customer_id){
        if($customer_id==0){
            $data['status']=0;
            $data['info']='参数错误';
            return $data;
        }
        $map['customer_id']=$customer_id;
        $field='activity_id,generalize_member_id,ticket_level,join_time,is_signin,is_order';
        $join=M('service_activity_join')->field($field)->where($map)->select();
        if(empty($join)){
            $data['status']=0;
            $data['info']='未报名过任何活动';
            return $data;
        }else{
            $ticket_arr = array('普通票','VIP');
            $signin_arr = array('未签到','已签到');
            $order_arr = array('未下单','已下单');
            $list=array();
            for ($i=0;$i<count($join);$i++) {
                $ticket_level_text=isset($ticket_arr[$join[$i]['ticket_level']])?$ticket_arr[$join[$i]['ticket_level']]:'';
                $is_signin = isset($signin_arr[$join[$i]['is_signin']])?$signin_arr[$join[$i]['is_signin']]:'';
                $is_order = isset($order_arr[$join[$i]['is_signin']])?$order_arr[$join[$i]['is_signin']]:'';
                $list[$i]['activity_name'] = getActivityName($join[$i]['activity_id']);
                $list[$i]['generalize_member_name'] = getUserName($join[$i]['generalize_member_id']);
                $list[$i]['ticket_level_text'] = $ticket_level_text;
                $list[$i]['join_time'] = time_format($join[$i]['join_time']);
                $list[$i]['is_signin'] = $is_signin;
                $list[$i]['is_order'] = $is_order;
            }
            $data['status']=1;
            $data['data']=$list;
            return $data;
        }
    }


    /**
     * @todo 获取正在进行的活动报名
     * @author fzh<fanzhaohai@hrbjjhh.com>
     * @since 2015-9-7
     */
    public function getActivityJoin($activity_id,$customer_id){
        if($activity_id==0||$customer_id==0){
            $this->error('参数错误');
            exit();
        }
        $obj = D('Admin/ServiceActivityJoin');
        $phone='';
        $result = $obj->getActivityJoinShow($activity_id,$phone,$customer_id);
        if($result['status']==0){
            return null;
        }else{
            return $result['data'];
        }

    }

    /**
     * @todo 获取装修需求
     * @author fzh<fanzhaohai@hrbjjhh.com>
     * @since 2015-9-8
     */
    public function getCustomerDemand($customer_id){
        if($customer_id == 0){
            $this->error = '用户id错误';
            return false;
        }
        $deman = D('ServiceApi')->myDemand($customer_id);
        if(empty($deman)){
            return null;
        }else{
            $customerDemand = array_column($deman, 'demand_content_id');
            if(!empty($customerDemand)){
                return implode(',',$customerDemand);
            }else{
                return null;
            }
        }
    }

    /**
     * @todo   保存用户需求
     * @author fzh<fanzhaohai@hrbjjhh.com>
     * @since 2015-9-6
     */
    public function saveCustomerDemand($customer_id,$demand_content_id){
        if($customer_id==0){
            $this->error='参数错误';
            exit();
        }
        //先删除装修需求
        $customer_demand=M('service_customer_demand');
        $customer_demand->where(array('customer_id'=>$customer_id))->delete();

        //处理二维数组，转换成一维数组，插入个人装修需求表
        $data=array();
        if(is_array($demand_content_id) && count($demand_content_id)>0){
            foreach ($demand_content_id as $key => $val){
                foreach($val as $k => $v){
                    $data[]=array(
                        'customer_id'=>$customer_id,
                        'demand_content_id'=>$v,
                        'demand_id'=>$key
                    );
                }
            }
        }

        if(!empty($data)){
            $customer_demand->addAll($data);
        }
        S('service_my_demand',null);
        //保存操作日志
        D('ServiceApi')->operationLog($customer_id,1,'修改装修需求');
        action_log('customerDemand_update', 'ServiceSeat', $customer_id, UID);
    }

    /**
     * @todo 活动报名
     * @author fzh<fanzhaohai@hrbjjhh.com>
     * @since 2015-9-9
     * @param $activity_info 报名信息
     *        $activity_id 活动id
     *        $phone_number 手机号， 只有票点能用到
     *        $customer_id 用户id
     *        $ticket_level 票别
     *        $sale_point_id 销售网点
     *        $bus_point_id 班车网点
     *        $customer_level 用户级别
     *        $departure_time_id //班车日期
     *        $people_num   //班车人数
     */
    public function addActivityJoin($activity_info,$activity_id,$customer_id,$ticket_level=0,$departure_time_id=array(),$people_num=array(),$customer_level=0,$bus_point_id=0,$phone_number='',$sale_point_id=0){
        
        if(empty($activity_info)){
            $this->error='查看报名信息错误';
            return false;
        }

        if((int)$customer_id==0){
            $this->error='用户id错误';
            return false;
        }
        if((int)$activity_id==0){
            $this->error='活动id错误';
            return false;
        }
        if(!in_array($customer_level, array(1,2,3))){
            $this->error='客户类别错误';
            return false;
        }

        if($ticket_level==''){
            $this->error='请选择票别';
            return false;
        }

        if((int)$ticket_level != 0 && (int)$ticket_level != 1){
            $this->error='票别错误';
            return false;
        }

        if(!empty($phone_number)){
            if(preg_match('/^1[3-8][0-9]{9}$/', $phone_number)){
                $this->error='手机号码错误';
                return false;
            }
        }

        //推广人
        $generalize_member_id=UID;
        //如果传入票点信息，渠道直接修改为票点  0:主动报名;1:票点报名;2:代报名;3:活动现场报名;4:推广报名
        $sign_channel = (int)$sale_point_id==0 ? 2 : 1;

        //查看报名信息的状态， 确定进行报名 / 转化 / 还是修改信息
        if($activity_info['status']>0){
            //已经报名过活动过的信息修改
            //如果已经报名 只修改报名信息
            if($activity_info['status']==1){
                $service_activity_join=D('ServiceActivityJoin');
                $return = $service_activity_join->changeActivityJoin($activity_info['data']['activity_join_id'],$ticket_level,$customer_level);
                if(false === $return){
                    $this->error = $service_activity_join->getError();
                    return false;
                }
            }
            //未报过名 先进行报名
            if($activity_info['status']==2){
                $service_activity_join = D('ServiceActivityJoin');
                $join = $service_activity_join->getAddActivityJoin($activity_id,$sign_channel,$phone_number,$customer_id,$ticket_level,$sale_point_id,$generalize_member_id,$customer_level);
                if($join === false){
                    $this->error = '报名失败，原因：'.$service_activity_join->getError();
                    return false;
                }else{
                    $activity_data = $join;
                }
                
                //保存操作日志
                if($ticket_level==1){
                    $ticket_level_text='VIP';
                }else{
                    $ticket_level_text='普通票';
                }
                D('ServiceApi')->operationLog($customer_id,5,'活动报名['.getActivityName($activity_id).']['.$ticket_level_text.']');
                action_log('activityJoin_add', 'ServiceActivityJoin', $activity_data['activity_join_id'], UID);
            }

            //报名VIP票后保存vip信息
            if($ticket_level==1){
                $expressDetail=$this->saveExpressDetail($activity_id,$customer_id);
                if($expressDetail === false){
                    if(empty($this->error)){
                        $this->error = 'VIP邮寄信息保存失败';
                    }
                    return false;
                }
                action_log('expressDetail_update', 'ServiceSeat', $customer_id, UID);
            }
            
            //修改班车信息
            if((int)$bus_point_id > 0){
                $activityJoinBusDemand = $this->saveActivityJoinBusDemand($customer_id,$activity_id,UID,$bus_point_id,$departure_time_id,$people_num);
                if($activityJoinBusDemand === false){
                    if(empty($this->error)){
                        $this->error = '班车信息保存失败';
                    }
                    return false;
                }
                action_log('activityJoinBusDemand_update', 'ServiceSeat', $customer_id, UID);
            }
        }else{
            $this->error=$activity_info['info'];
            return false;
        }


    }

    /**
     * @todo 修改VIP信息
     * @author fzh<fanzhaohai@hrbjjhh.com>
     * @since 2015-9-9
     */
    public function saveExpressDetail($activity_id,$customer_id){
        //验证所有表单，如果有一项为填写状态那么就 不增加vip邮寄信息
        $attr = true;
        if(!empty(I('post.receiving_user',''))){
            $attr = false;
        }
        if(!empty(I('post.receiving_phone_number',''))){
            $attr = false;
            
        }
        if(!empty(I('post.receiving_address',''))){
            $attr = false;
        }
        if(!empty(I('post.express_id',''))){
            $attr = false;
        }
        if(!empty(I('post.courier_number',''))){
            $attr = false;
        }
        if(!empty(I('post.mail_status',''))){
            $attr = false;
        }
        if(!$attr){
            $_POST['activity_id'] = (int)$activity_id;
            $_POST['member_id'] = UID;
            $_POST['customer_id'] = (int)$customer_id;
            $obj=D('ServiceExpressDetail');
            
            //如果VIP邮寄信息存在的话， 模拟post数据
            $data = $obj->field('express_detail_id')->where(array('activity_id'=>(int)$activity_id,'customer_id'=>(int)$customer_id))->find();
            if(!empty($data)){
                $_POST['express_detail_id'] = $data['express_detail_id'];
            }
            
            if(false === $obj->update()){
                $this->error = $obj->getError();
                return false;
            }else{
                D('ServiceApi')->operationLog($customer_id,8,'Vip邮寄信息修改');
                return true;
            }
        }
    }

    /**
     * @todo 设置为虚报
     * @author fzh<fanzhaohai@hrbjjhh.com>
     * @since 2015-9-10
     */
    public function isFalse($activity_join_id){
        if((int)$activity_join_id==0){
            $this->error='活动报名id错误';
            return false;
        }
        $map=array('activity_join_id'=>$activity_join_id);
        $data=array('is_false'=>1);
        $return = M('service_activity_join')->where($map)->save($data);
        if($return===false){
            $this->error='设置虚报失败';
            return  false;
        }else{
            return true;
        }
    }
    
    /**
     * @todo 获取最后通话时间
     * @author fzh
     * @since 2015-9-15
     */
    public function getLastCallTime($customer_id) {
        if($customer_id==0){
            $this->error='用户id错误';
            return false;
        }
        $service_call_log = M('service_call_log')->field('create_time')->where(array('customer_id'=>$customer_id))->order(' create_time desc')->find();
        if(empty($service_call_log)){
            return '';
        }else{
            return $service_call_log['create_time'];
        }
    }
    
    /**
     * @todo 验证用户的绑定人，是否和操作人一致(这里只验证有绑定人的客户)
     * @author fzh
     */
    public function getBindMember($customer_id,$type=0){
        $customer_id = (int)$customer_id;
        if($customer_id==0){
            $this->error = array('info'=>'用户id错误');
            return false;
        }
        $service_my_customer = M('service_my_customer')->field('member_id')->where(array('customer_id'=>$customer_id))->find();
        if(empty($service_my_customer)){
            return true;
        }
        if($service_my_customer['member_id'] != UID){
            $this->error = array('id'=>$service_my_customer['member_id'],'info'=>'此用户已经被其他坐席绑定，请不要操作别人的用户');
            return false;
        }
        
    }
    
    
}
